package cpre556.project2.eclipsePlugin.support;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.util.Vector;

public class XMLIgnoreReader {
	
	private Saver saver = null;
	
	public XMLIgnoreReader(Saver s){
		saver = s;
	}
	
	@SuppressWarnings("unused")
	private Vector<String> toVector(String str){
		int strLength = str.length();
		Vector<String> returnVector = new Vector<String>();
				
		for(int i=0; i<strLength; i++)
			returnVector.add(i, str.substring(i, i+1));
		
		return returnVector;
	}
	
	public Vector<IgnoredVulnerability> getIgnoredVulnerabilitiesVector(){
		FileInputStream fis = null;
		BufferedReader reader = null;
		
		Vector<IgnoredVulnerability> ignoredVulnerabilities = new Vector<IgnoredVulnerability>();
		IgnoredVulnerability ignoredVulnerability;
		
		try{
			fis = new FileInputStream(saver.getIgnore());
			reader = new BufferedReader(new InputStreamReader(fis));
			
			reader.readLine();
			reader.readLine();
			
			String inLine = reader.readLine();
			
			while(!inLine.equalsIgnoreCase("</Vulnerability-Ignore-List>")){                       
				
				ignoredVulnerability = new IgnoredVulnerability();
								
				// inLine is <Filename>
				inLine = reader.readLine();
				
				// inLine is <Filename> data
				inLine = reader.readLine();
				ignoredVulnerability.setFilename(inLine.trim());
				
				// inLine is </Filename>
				inLine = reader.readLine();
				
				// inLine is <LineNumber>
				inLine = reader.readLine();
				
				// inLine is <LineNumber> data
				inLine = reader.readLine();
				ignoredVulnerability.setLineNumber(Integer.valueOf(inLine.trim()).intValue());
				
				// inLine is </LineNumber>
				inLine = reader.readLine();
				
				// inLine is <Priority>
				inLine = reader.readLine();
				
				// inLine is <Priority> data
				inLine = reader.readLine();
				ignoredVulnerability.setPriority(inLine.trim());
				
				// inLine is </Priority>
				inLine = reader.readLine();
				
				// inLine is <Function>
				inLine = reader.readLine();
				
				// inLine is <Function> data
				inLine = reader.readLine();
				ignoredVulnerability.setFunctionName(inLine.trim());
				
				// inLine is </Function>
				inLine = reader.readLine();
				
				// inLine is <SourceCode>
				inLine = reader.readLine();
				
				// inLine is <SourceCode> data
				inLine = reader.readLine();
				ignoredVulnerability.setLineOfCode(inLine.trim());
				
				// inLine is </SourceCode>
				inLine = reader.readLine();
				
				// inLine is <Description>
				inLine = reader.readLine();
				
				// inLine is <Description> data
				inLine = reader.readLine();
				ignoredVulnerability.setDescription(inLine.trim());
				
				// inLine is </Description>
				inLine = reader.readLine();
				
				// inLine is <Suggestion>
				inLine = reader.readLine();
				
				// inLine is <Suggestion> data
				inLine = reader.readLine();
				ignoredVulnerability.setSuggestion(inLine.trim());
			
				// inLine is </Sugestion>
				inLine = reader.readLine();
				
				// inLine is <Pred1>
				inLine = reader.readLine();
				
				// inLine is <Pred1> data
				inLine = reader.readLine();
				ignoredVulnerability.setPredecessor1(inLine.trim());
				
				// inLine is </Pred1>
				inLine = reader.readLine();
				
				// inLine is <Pred2>
				inLine = reader.readLine();
				
				// inLine is <Pred2> data
				inLine = reader.readLine();
				ignoredVulnerability.setPredecessor2(inLine.trim());
				
				// inLine is </Pred2>
				inLine = reader.readLine();
				
				// inLine is <Pred3>
				inLine = reader.readLine();
				
				// inLine is <Pred3> data
				inLine = reader.readLine();
				ignoredVulnerability.setPredecessor3(inLine.trim());
				
				// inLine is </Pred3>
				inLine = reader.readLine();
				
				// inLine is <Suc1>
				inLine = reader.readLine();
				
				// inLine is <Suc1> data
				inLine = reader.readLine();
				ignoredVulnerability.setSuccessor1(inLine.trim());
				
				// inLine is </Suc1>
				inLine = reader.readLine();
				
				// inLine is <Suc2>
				inLine = reader.readLine();
				
				// inLine is <Suc2> data
				inLine = reader.readLine();
				ignoredVulnerability.setSuccessor2(inLine.trim());
				
				// inLine is </Suc2>
				inLine = reader.readLine();
				
				// inLine is <Suc3>
				inLine = reader.readLine();
				
				// inLine is <Suc3> data
				inLine = reader.readLine();
				ignoredVulnerability.setSuccessor3(inLine.trim());
				
				// inLine is </Suc3>
				inLine = reader.readLine();
				
				//inLine is </Vulnerbility>
				inLine = reader.readLine();
				
				ignoredVulnerabilities.add(ignoredVulnerability);
				
				inLine = reader.readLine();				
			}		     		    
		}
		catch(FileNotFoundException e1){
			//do nothing
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
		return ignoredVulnerabilities;
	}
}
